package com.mls.guardian.project;

import com.mls.guardian.config.helper.ConfigHandler;
import com.mls.guardian.config.helper.ConfigPathManagr;
import com.mls.guardian.config.helper.JsonConfigHelper;
import com.mls.guardian.config.ProjectConfig;
import com.mls.guardian.em.RevisionType;
import com.mls.guardian.exception.ProjectConfigErrorException;
import org.apache.commons.exec.util.MapUtils;

import java.util.*;

/**
 * Created by jl on 15/10/21.
 */
public class ProjectConfigFactory {
    public static List<ProjectConfig> createProjects() throws Exception {
        ConfigHandler jsonConfigHelper = new JsonConfigHelper();
        Map map = (Map) jsonConfigHelper.parse(ConfigPathManagr.getProjectConfigPath());


        Set keys = map.keySet();

        Iterator<String> its = keys.iterator();
        List<ProjectConfig> projects = new ArrayList<ProjectConfig>();
        while (its.hasNext()) {
            String key = its.next();
            Map projectMap = (Map) map.get(key);

            ProjectConfig projectObj = new ProjectConfig();
            projectObj.setProjectName(key);
            String projectPath = (String) projectMap.get("path");
            String revisionAddress = (String) projectMap.get("revisionAddress");

            String revisionTypeStr = "";

            if(projectMap.containsKey("revisionType") && projectMap.get("revisionType") != null){
                revisionTypeStr = (String) projectMap.get("revisionType");
            }

            //优先取项目配置,如果没有取config.properties配置
            String springActive = "";
            if (projectMap.get("springActive") != null) {
                springActive = (String) projectMap.get("springActive");
            }


            if (projectPath == null) {
                throw new ProjectConfigErrorException(key + "对应的path未配置，添加配置！！！");
            }
            projectObj.setProjectPath(projectPath);
            projectObj.setRevisionAddress(revisionAddress);
            if(revisionTypeStr.equals("SVN")){
                projectObj.setRevisionType(RevisionType.SVN);
            }else {
                projectObj.setRevisionType(RevisionType.GIT);
            }

            projectObj.setSpringActive(springActive);
            projectObj.setJarModuleList(parseJarModule(key, projectMap));
            projectObj.setJvmArgs(parseJvmArgs(key, projectMap));
            projectObj.setWebModuleList(parseWebModule(key, projectMap));
            projectObj.setWebAliasList(parseWebAlias(key, projectMap));
            projectObj.setTomcatHomeList(parseTomcatHome(key, projectMap));
            projectObj.setReplaceFileMap(parseReplaceFile(key, projectMap));
            projects.add(projectObj);
        }

        return projects;
    }

    private static List<String> parseTomcatHome(String projectName, Map map) throws ProjectConfigErrorException {
        List<String> obj = (List<String>) map.get("tomcatHome");

        if (obj == null) {
            throw new ProjectConfigErrorException(projectName + "对应的tomcatHome未配置，添加配置！！！");
        }
        return obj;
    }

    private static List<String> parseJvmArgs(String projectName, Map map) throws ProjectConfigErrorException {
        List<String> obj = (List<String>) map.get("jvmArgs");

        if (obj == null) {
            throw new ProjectConfigErrorException(projectName + "对应的jvmArgs未配置，添加配置！！！");
        }
        return obj;
    }

    private static List<String> parseJarModule(String projectName, Map map) throws ProjectConfigErrorException {
        List<String> obj = (List<String>) map.get("jarModule");

        if (obj == null) {
            throw new ProjectConfigErrorException(projectName + "对应的jarModule未配置，添加配置！！！");
        }
        return obj;
    }

    private static List<String> parseWebModule(String projectName, Map map) throws ProjectConfigErrorException {
        List<String> obj = (List<String>) map.get("webModule");

        if (obj == null) {
            throw new ProjectConfigErrorException(projectName + "对应的webModule未配置，添加配置！！！");
        }
        return obj;
    }

    private static List<String> parseWebAlias(String projectName, Map map) throws ProjectConfigErrorException {
        List<String> obj = (List<String>) map.get("webAlias");

        if (obj == null) {
            throw new ProjectConfigErrorException(projectName + "对应的webAlias未配置，添加配置！！！");
        }
        return obj;
    }

    private static Map<String, String> parseReplaceFile(String projectName, Map map) throws ProjectConfigErrorException {
        Map<String, String> mapFile = (Map<String, String>) map.get("replaceFile");

        if (mapFile == null) {
            throw new ProjectConfigErrorException(projectName + "对应的replaceFile未配置，添加配置！！！");
        }
        return mapFile;
    }
}
